﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace ObjectBakery.Helpers
{
    public class AssemblyHelper
    {
        public static string CopyXmlSerializableResourceToTempFile(Assembly assy, string strResource)
        {

            string ns = GetFullyQualifiedNameSpace(assy);

            string temp = "";

            Stream stream = assy.GetManifestResourceStream(ns + '.' + strResource);
            if (stream != null)
            {
                StreamReader sr = new StreamReader(stream);
                temp = Path.GetTempFileName() + Guid.NewGuid().ToString();
                StreamWriter sw = new StreamWriter(temp, false);
                sw.Write(sr.ReadToEnd());
                sw.Flush();
                sw.Close();
            }
            return temp;
        }
        public static string CopyBinarySerializableResourceToTempFile(Assembly assy, string relativePath)
        {

            Stream stm = GetResourceAsMemoryStream(assy, relativePath);

            string temp = string.Empty;

            // Resource item exists. 
            if (stm != null)
            {
                // Create byte array and read resource file into it. 
                byte[] bytes = new byte[stm.Length];
                BinaryReader br = new BinaryReader(stm);
                br.Read(bytes, 0, bytes.Length);

                temp = Path.GetTempFileName();

                // Create file on disk drive and write byte array into it. 
                FileStream fs = new FileStream(@temp, FileMode.Open, FileAccess.ReadWrite);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(bytes, 0, bytes.Length);


                // Clean up. 
                bw.Flush();
                bw.Close();
            }


            return temp;
        }

        public static MemoryStream GetResourceAsMemoryStream(Assembly assy, string relativePath)
        {

            MemoryStream ms = new MemoryStream();

            string ns = GetFullyQualifiedNameSpace(assy);


            // Get embedded resource item. 
            Stream stm = assy.GetManifestResourceStream(ns + '.' + relativePath);

            // Resource item exists. 
            if (stm != null)
            {
                // Create byte array and read resource file into it. 
                byte[] bytes = new byte[stm.Length];
                BinaryReader br = new BinaryReader(stm);
                br.Read(bytes, 0, bytes.Length);
                ms = new MemoryStream(bytes);
            }
            else
            {
                throw new Exception("Couldn't find embedded resource.");
            }

            return ms;
        }

        /// <summary>
        /// Gets the fully qualified root namespace of an assembly with dot appended
        /// </summary>
        /// <param name="assy"></param>
        /// <returns></returns>
        public static string GetFullyQualifiedNameSpace(Assembly assy)
        {
            string ns = assy.FullName.Split(',')[0];
            if (ns == null)
                ns = "";

            return ns;
        }
    }
}
